angular.module('myapp.SPAService', []).factory('SPAService', function($http, $rootScope, $cordovaVibration, $cordovaToast, $timeout, PopupService, $state, $myconfig, BthSevice) {


    return {
        title: '',
        upKeepData: { //焕颜宝保养数据
            ThisTime: 0, //记录当前时间
            typeTime: 0, //保养时长,
            ts: 0, //记录当前保养到哪个时间点了，倒计时
            maintain_sta: 0, //保养状态
            HYBsec: 0 //保养时间
        },
        coryzaData: {
            ThisTime: 0, //记录当前时间
            typeTime: 0, //保养时长,
            ts: 0, //记录当前保养到哪个时间点了，倒计时
            maintain_sta: 0, //保养状态
            BGBsec: 0 //保养时间
        }, //存放鼻腔宝当前保养的数据 用户返回其它页面再进来后可持续
        setCurrentData: function(title, duration, user_id, deviceName, mode, state) {
            this.title = title;
            // this.duration = duration;
            this.coryzaData.user_id = user_id;
            this.coryzaData.deviceName = deviceName;
            this.coryzaData.mode = mode;
            this.coryzaData.state = state;
        },
        setYZBCurrentData: function(title, duration, user_id, deviceName, mode, state) {
            this.title = title;
            // this.duration = duration;
            this.upKeepData.user_id = user_id;
            this.upKeepData.deviceName = deviceName;
            this.upKeepData.mode = mode;
            this.upKeepData.state = state;
        },
        backgroundRunning: function(isSchedule, createtime, callback) { //保存保养记录
            localStorage.removeItem('lastSPAEnd');

            this.playMessage(); //播放背景音乐
            $cordovaVibration.vibrate(1000); //震动3秒
            $timeout(function () {
                $cordovaVibration.vibrate(800); //震动3秒
            }, 1500);
            $timeout(function () {
                $cordovaVibration.vibrate(1000); //震动3秒
            }, 3000);

            BthSevice.closeLED(); // 完成后关闭led

            if(this.coryzaData.thePage){ //如果是在保养页面取消通知定时器
                cordova.plugins.notification.local.cancel(1000, function () {
                    // Notification was cancelled
                    console.log('取消鼻光宝宝定时器');
                });
            }

            var _this = this;
            // _this.BGBsec = 0;
            var user_id = this.coryzaData.user_id;
            var mode = this.coryzaData.mode;
            var deviceName = this.coryzaData.deviceName;
            var BGBsec;
            if(isSchedule){
                BGBsec = this.coryzaData.typeTime;
            }else{
                BGBsec = this.coryzaData.BGBsec;
            }

            var state = this.coryzaData.state;

            this.coryzaData = { stateText: '开始' };
            // $rootScope.start = false; //停止运行

            $http.post($myconfig.api.base_host + '/users/maintain', {
                "user_id": user_id,
                "dev_name": deviceName,
                "mode": mode,
                "duration": BGBsec,
                "state": state,
                "createtime": createtime
            }).then(function(res) {
                // console.log(res.data.data);
                // $interval.cancel(secTime);
                var integral = res.data.integral; //本次健康豆
                $http.get($myconfig.api.base_host + '/physicallogs/finsh?user_id=' + user_id + '&mode=' + mode + '&dev_name=' + deviceName)
                    .then(function(res) {
                        // console.log(res.data.data);
                        res.data.data.duration = ((res.data.data.duration) / 60).toFixed(1);
                        res.data.data.totle_duration = ((res.data.data.totle_duration) / 60).toFixed(1);
                        res.data.data.num = integral; //健康豆
                        $rootScope.finsh = res.data.data;
                        console.log($rootScope.finsh);
                        $http.get($myconfig.api.base_host + '/users/user_info?mobile=' + $rootScope._RootUser.mobile)
                            .then(function(res) {
                                if (res.data.success) {
                                    localStorage.setItem('user', JSON.stringify(res.data.data));
                                }

                                callback();
                            });
                    });
            });
        },
        backgroundRunningYZB: function(isSchedule, createtime, callback) { //保存保养记录
            localStorage.removeItem('lastSPAEnd');

            this.playMessage(); //播放背景音乐
            $cordovaVibration.vibrate(1000); //震动3秒
            $timeout(function () {
                $cordovaVibration.vibrate(800); //震动3秒
            }, 1500);
            $timeout(function () {
                $cordovaVibration.vibrate(1000); //震动3秒
            }, 3000);

            BthSevice.closeLED(); // 完成后关闭led

            if(this.upKeepData.thePage){ //如果是在保养页面取消通知定时器
                cordova.plugins.notification.local.cancel(1001, function () {
                    // Notification was cancelled
                    console.log('取消焕颜宝定时器');
                });
            }
            var _this = this;
            // _this.BGBsec = 0;

            var user_id = this.upKeepData.user_id;
            var mode = this.upKeepData.mode;
            var deviceName = this.upKeepData.deviceName;
            var HYBsec;
            if(isSchedule){
                HYBsec = this.upKeepData.typeTime;
            }else{
                HYBsec = this.upKeepData.HYBsec;
            }
            var state = this.upKeepData.state;
            $rootScope.start = false;
            this.upKeepData = { stateText: '开始' };
            // $rootScope.start = false; //停止运行

            $http.post($myconfig.api.base_host + '/users/maintain', {
                "user_id": user_id,
                "dev_name": deviceName,
                "mode": mode,
                "duration": HYBsec,
                "state": state,
                "createtime": createtime
            }).then(function(res) {
                // console.log(res.data.data);
                // $interval.cancel(secTime);
                var integral = res.data.integral; //本次健康豆
                $http.get($myconfig.api.base_host + '/physicallogs/finsh?user_id=' + user_id + '&mode=' + mode + '&dev_name=' + deviceName)
                    .then(function(res) {
                        // console.log(res.data.data);
                        res.data.data.duration = ((res.data.data.duration) / 60).toFixed(1);
                        res.data.data.totle_duration = ((res.data.data.totle_duration) / 60).toFixed(1);
                        res.data.data.num = integral; //健康豆
                        $rootScope.finsh = res.data.data;
                        console.log($rootScope.finsh);
                        $http.get($myconfig.api.base_host + '/users/user_info?mobile=' + $rootScope._RootUser.mobile)
                            .then(function(res) {
                                if (res.data.success) {
                                    localStorage.setItem('user', JSON.stringify(res.data.data));
                                }

                                callback();
                            });
                    });
            });
        },
        goCoryzaEnd: function() {
            $state.go('app.CoryzaEnd', {
                type: this.title
            });
        },
        //播放背景音乐
        playMessage: function () {
            window.plugins.NativeAudio.preloadComplex( 'music', 'sounds/message.mp3', 1, 1, 0, function(msg){
                console.log('success: ' + msg);
                //加载成功后播放
                window.plugins.NativeAudio.loop( 'music' );
                $timeout(function(){
                    window.plugins.NativeAudio.stop( 'music' );
                    window.plugins.NativeAudio.unload( 'music' );

                }, 3500);
            }, function(msg){
                console.log( 'error: ' + msg );
            });

        }
    };
});
